﻿/*
 * EmeCoder v1.2.1.168
 * 作者：Administrator/PC4APPLE
 * 时间：2013-07-26 15:18:51
 * 版权：版权所有 (C) Eme Development Team 2013
*/

﻿using System;
using System.ComponentModel;
using HmFramework.Data;

namespace CuteSprite.Eme.Data.CRM
{
	/// <summary>快件英文描述</summary>
	[Serializable]
	[ModelCheckMode(ModelCheckModes.CheckTableWhenFirstUse)]
	public class ExpressDescriptionEN : ExpressDescriptionEN<ExpressDescriptionEN> { }

	/// <summary>快件英文描述</summary>
	public partial class ExpressDescriptionEN<TEntity> : Entity<TEntity> where TEntity : ExpressDescriptionEN<TEntity>, new()
	{
		#region 对象操作﻿

		static ExpressDescriptionEN()
		{
			// 用于引发基类的静态构造函数，所有层次的泛型实体类都应该有一个
			TEntity entity = new TEntity();
		}

		/// <summary>已重载，保存实体</summary>
		/// <returns></returns>
		public override int Save()
		{
			// 如果注释已经存在，直接退出
			if (!EntityHelper.IsORMRemoting && CheckDescriptionIsExist(DescriptionEN, ID)) { return ID; }

			return base.Save();
		}

		#endregion

		#region 扩展属性﻿

		#endregion

		#region 扩展查询﻿

		/// <summary>根据主键查找</summary>
		/// <param name="id">主键</param>
		/// <returns></returns>
		[DataObjectMethod(DataObjectMethodType.Select, false)]
		public static TEntity FindByID(Int32 id)
		{
			if (Meta.Count > 1000)
				return Find(_.ID, id);
			else // 实体缓存
				return Meta.Cache.Entities.Find(_.ID, id);
			// 单对象缓存
			//return Meta.SingleCache[id];
		}

		/// <summary>根据英文描述查找</summary>
		/// <param name="descriptionen">英文描述</param>
		/// <returns></returns>
		[DataObjectMethod(DataObjectMethodType.Select, false)]
		public static TEntity FindByDescriptionEN(String descriptionen)
		{
			if (Meta.Count > 1000)
				return Find(_.DescriptionEN, descriptionen);
			else // 实体缓存
				return Meta.Cache.Entities.Find(_.DescriptionEN, descriptionen);
			// 单对象缓存
			//return Meta.SingleCache[descriptionen];
		}

		#endregion

		#region 高级查询

		// 以下为自定义高级查询的例子

		///// <summary>
		///// 查询满足条件的记录集，分页、排序
		///// </summary>
		///// <param name="key">关键字</param>
		///// <param name="orderClause">排序，不带Order By</param>
		///// <param name="startRowIndex">开始行，0表示第一行</param>
		///// <param name="maximumRows">最大返回行数，0表示所有行</param>
		///// <returns>实体集</returns>
		//[DataObjectMethod(DataObjectMethodType.Select, true)]
		//public static EntityList<TEntity> Search(String key, String orderClause, Int32 startRowIndex, Int32 maximumRows)
		//{
		//    return FindAll(SearchWhere(key), orderClause, null, startRowIndex, maximumRows);
		//}

		///// <summary>
		///// 查询满足条件的记录总数，分页和排序无效，带参数是因为ObjectDataSource要求它跟Search统一
		///// </summary>
		///// <param name="key">关键字</param>
		///// <param name="orderClause">排序，不带Order By</param>
		///// <param name="startRowIndex">开始行，0表示第一行</param>
		///// <param name="maximumRows">最大返回行数，0表示所有行</param>
		///// <returns>记录数</returns>
		//public static Int32 SearchCount(String key, String orderClause, Int32 startRowIndex, Int32 maximumRows)
		//{
		//    return FindCount(SearchWhere(key), null, null, 0, 0);
		//}

		/// <summary>构造搜索条件</summary>
		/// <param name="key">关键字</param>
		/// <returns></returns>
		private static String SearchWhere(String key)
		{
			// WhereExpression重载&和|运算符，作为And和Or的替代
			// SearchWhereByKeys系列方法用于构建针对字符串字段的模糊搜索
			var exp = SearchWhereByKeys(key, null);

			// 以下仅为演示，Field（继承自FieldItem）重载了==、!=、>、<、>=、<=等运算符（第4行）
			//if (userid > 0) exp &= _.OperatorID == userid;
			//if (isSign != null) exp &= _.IsSign == isSign.Value;
			//if (start > DateTime.MinValue) exp &= _.OccurTime >= start;
			//if (end > DateTime.MinValue) exp &= _.OccurTime < end.AddDays(1).Date;

			return exp;
		}

		#endregion

		#region 扩展操作

		#endregion

		#region 业务

		/// <summary>根据英文描述查找</summary>
		/// <param name="descriptionen">英文描述</param>
		/// <param name="excludedID">排除实例本身ID</param>
		/// <returns></returns>
		[DataObjectMethod(DataObjectMethodType.Select, false)]
		public static Boolean CheckDescriptionIsExist(String descriptionen, Int32 excludedID)
		{
			TEntity entity = null;
			if (excludedID > 0)
			{
				if (Meta.Count > 1000)
				{
					entity = Find(_.DescriptionEN.Equal(descriptionen) & _.ID.NotEqual(excludedID));
				}
				else
				{
					entity = Meta.Cache.Entities.Find(e => (e.DescriptionEN == descriptionen && e.ID != excludedID));
				}
			}
			else
			{
				entity = FindByDescriptionEN(descriptionen);
			}
			return entity != null;
		}

		#endregion
	}
}